package com.itheima.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.common.R;
import com.itheima.common.TokenHolder;
import com.itheima.pojo.Orders;
import com.itheima.pojo.PageBean;
import com.itheima.service.OrdersService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 小帅蓝
 * @Date 2022/6/27 17:22
 */
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Resource
    private OrdersService ordersService;

    @PostMapping("/submit")
    public R<String> insertOrder(@RequestBody InsertOrderDTO insertOrderDTO) {
        Orders orders = new Orders();
        BeanUtil.copyProperties(insertOrderDTO, orders);
        ordersService.insertOrder(orders);
        return R.success("订单提交成功！");
    }

    @GetMapping("/userPage")
    public R<PageBean> getOrderList(Integer page, Integer pageSize) {
        List<OrderListVO> orderList = ordersService.getOrderAndDetailsList(page, pageSize);
        Integer total = ordersService.getCount(null, TokenHolder.getCurrentId());
        return R.success(new PageBean(orderList, total, pageSize, page));
    }

}
